﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace Mimoza.VNCPlugin
{
    public class ExecutePlugin : Mimoza.PluginLayer.IExecutePlugin
    {
        [DllImport("Mimoza.vncpassword.dll", CharSet = CharSet.Auto)]
        private static extern int SetVNCPassword(byte[] password);

        private void StorDataInRegistry(string data)
        {
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Mimoza", true))
            {
                if (key != null)
                {
                    key.SetValue("VNCPlugin", data);
                }
            }
        }
        #region IExecutePlugin Members

        public string PluginId
        {
            get { return this.ToString(); }
        }

        public void Add(Mimoza.ObjectLayer.Policy policy)
        {
            Add(policy, GetData(policy.Data));
            StorDataInRegistry(policy.Data);
        }

        public void Remove(Mimoza.ObjectLayer.Policy policy)
        {
            Remove(policy, GetData(policy.Data));
            StorDataInRegistry("");
        }

        public void Update(Mimoza.ObjectLayer.Policy policy)
        {
            Update(policy, GetData(policy.Data));
            StorDataInRegistry(policy.Data);
        }

        #endregion

        void Add(Mimoza.ObjectLayer.Policy policy, VNCPluginData data)
        {
            Trace("Add", policy, data);
            ApplyVNCPolicy(policy, data);
        }

        void Update(Mimoza.ObjectLayer.Policy policy, VNCPluginData data)
        {
            Trace("Update", policy, data);
            ApplyVNCPolicy(policy, data);
        }

        void Remove(Mimoza.ObjectLayer.Policy policy, VNCPluginData data)
        {
            Trace("Remove", policy, data);
        }

        void ApplyVNCPolicy(Mimoza.ObjectLayer.Policy policy, VNCPluginData data)
        {
            Mimoza.Common.Instrumental.Registry registry = new Mimoza.Common.Instrumental.Registry(
                    "init");
            bool bApplyPswdRes = false;
            
            if (data.IsVncEnabled)
            {
                Common.Logger.Log.Info("Mimoza.SystemPlugin.ExecutePlugin.ApplyVncPassword pass - {0}.",
                    data.VNCPassword);

                registry.CheckAndRewrite("Launch91", "\\Windows\\WinVNC.exe");

                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                byte[] bytes = encoding.GetBytes(data.VNCPassword + " ");
                bytes[bytes.Length - 1] = 0;

#if WindowsCE
                if (SetVNCPassword(bytes)!=0) bApplyPswdRes = true;
#endif
            }
            else
            {
                registry.DeleteValue("Launch91");
            }

            Common.Logger.Log.Info("Mimoza.SystemPlugin.ExecutePlugin.ApplyVncPassword registry - {0}:{1}",
                    registry.IsModified, bApplyPswdRes);

            if (registry.IsModified || bApplyPswdRes) 
                Mimoza.PluginLayer.PluginResultStore.PutRestartResult();
        }
            
        void Trace(string operationName, Mimoza.ObjectLayer.Policy policy, VNCPluginData data)
        {
            Common.Logger.Log.Info("Mimoza.SystemPlugin.ExecutePlugin." + operationName + 
                " Name - '" + policy.Name + "'.");      
        }

        VNCPluginData GetData(string data)
        {
            return VNCPluginData.Deserialize(data);
        }
    }
}
